package com.dk.service.impl;

import com.dk.controller.AlipayController;
import com.dk.entity.DtAuctionItem;
import com.dk.entity.DtAuctionOrder;
import com.dk.entity.DtUser;
import com.dk.entity.DtUser;
import com.dk.mapper.DtAuctionItemMapper;
import com.dk.mapper.DtAuctionOrderMapper;
import com.dk.mapper.DtUserMapper;
import com.dk.service.IDtAuctionItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.service.IDtUserService;
import com.dk.util.JavaMailUtil;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 拍卖商品表，SKU表 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-12-20
 */
@Service
public class DtAuctionItemServiceImpl  implements IDtAuctionItemService {

    @Autowired
    private DtAuctionItemMapper mapper;

    @Autowired
    private DtAuctionOrderMapper dtAuctionOrderMapper;

    @Autowired
    private DtUserMapper dtUserMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private IDtUserService userService;
    @Autowired
    private JavaMailUtil javaMailUtil;
    @Autowired
    private AlipayController alipayController;

    @Override
    public void add(DtAuctionItem item) {
        int i = mapper.insert(item);

    }

    @Override
    public void updateStatus(Long id, String status) throws MessagingException {
        DtAuctionItem item =new DtAuctionItem();
        item.setId(id);
        item.setStatus(status);
        mapper.updateById(item);
        if ("2".equals(status) || "3".equals(status)){
            DtAuctionItem item1 = mapper.selectById(id);
            DtUser user = userService.selectById(item1.getSellerId());
            String message ="";
            if ("2".equals(status)){
                message= user.getEmail()+","+item1.getTitle()+"申请拍卖成功，请及时支付保证金";
            }else if ("3".equals(status)){
                message= user.getEmail()+","+item1.getTitle()+"申请拍卖审核失败";
            }
            System.out.println(message);
            String[] msgs = message.split(",");
            javaMailUtil.test("拍卖申请通知",msgs[1],msgs[0]);
            System.out.println("已发送邮件");
//        rocketMQTemplate.convertAndSend("auction",message);
        }

    }

    @Override
    public String sellerPay(Long id) {
        DtAuctionItem item = mapper.selectById(id);
        String s = alipayController.alipayTradePagePay(id.toString(), item.getSellerBondPrice().toString(), item.getTitle(), item.getSellPoint());

        return s;
    }

    @Override
    public List<DtAuctionItem> select() {
        List<DtAuctionItem> list = mapper.selectList(null);
        List<DtAuctionItem> items = new ArrayList<>();
        for (DtAuctionItem item : list) {
            Date date = new Date();
            long nowtime = date.getTime();
            long startime = nowtime-24*60*60*1000;
            long time1 = item.getSellTime().getTime();
            if ("4".equals(item.getStatus()) && time1>startime && time1<=nowtime){
                items.add(item);
            }

        }
        return items;
    }

    @Override
    public String userPay(Long id) {
        DtAuctionItem item = mapper.selectById(id);
        String s = alipayController.alipayTradePagePay(id.toString(), item.getUserBondPrice().toString(), item.getTitle(), item.getSellPoint());
        return s;
    }
//item 中的数据
    @Override
    public DtAuctionItem selectById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void insertOrder(DtAuctionOrder order) {
        dtAuctionOrderMapper.insert(order);
    }
//订单中的数据
    @Override
    public DtAuctionOrder selectOrderById(Long id) {
        return dtAuctionOrderMapper.selectById(id);
    }

    @Override
    public void updateItemStatus(DtAuctionItem item) {
        mapper.updateById(item);
    }
//修改order表
    @Override
    public void updateOrder(DtAuctionOrder order) {
        dtAuctionOrderMapper.updateById(order);
    }
//查询用户表  (测试)
    @Override
    public DtUser selectUserById(Long i) {
        return dtUserMapper.selectById(i);
    }
}
